import '../swagger.desc'
import 'reflect-metadata'
import {Router} from 'express'
import {mapRoute} from '../uitl'

import UserController from './userController'
import ToDoController from './toDoController'

const serverConfig = require('../server.config.json')
class InitRouterController {
  private _router: Router = Router()
  constructor() {
    this.formatRouter(UserController)
    this.formatRouter(ToDoController)
  }

  private formatRouter(Controller) {
    const rootUrl = Reflect.getMetadata('path', Controller)
    const routes = mapRoute(new Controller())
    const controllerRouter = Router()
    routes.forEach((item) => {
      controllerRouter[item.method](item.route, item.fn)
    })
    this._router.use(serverConfig.WEB_API.BASE_URL + rootUrl, controllerRouter)
  }

  public get router(): Router {
    return this._router
  }
}

export default new InitRouterController().router
